package com.hermit.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.autoconfigure.SpringBootVFS;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.config.GlobalConfig;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.sql.DataSource;

/**
 * mybatis-plus配置
 */
@EnableTransactionManagement
@Configuration
@MapperScan("com.hermit.system.dao")
public class MybatisPlusConfig {

    @Autowired
    private MybatisPlusProperties mybatisPlusProperties;

    @Value("${mybatis-plus.global-config.db-config.logic-delete-value}")
    private Integer logicDeleteValue;

    @Value("${mybatis-plus.global-config.db-config.logic-not-delete-value}")
    private Integer logicNotDeleteValue;

    @Bean
    @Primary
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        return druidDataSource;
    }

    @Bean
    @Primary
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
        factory.setDataSource(dataSource);
        factory.setVfs(SpringBootVFS.class);
        MybatisConfiguration configuration = mybatisPlusProperties.getConfiguration();
        if (configuration == null && !StringUtils.hasText(mybatisPlusProperties.getConfigLocation())) {
            configuration = new MybatisConfiguration();
        }
        factory.setConfiguration(configuration);
        if (mybatisPlusProperties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(mybatisPlusProperties.getConfigurationProperties());
        }
        // 分页插件
        factory.setPlugins(new Interceptor[]{new PaginationInterceptor()});
        if (StringUtils.hasLength(mybatisPlusProperties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(mybatisPlusProperties.getTypeAliasesPackage());
        }
        if (mybatisPlusProperties.getTypeAliasesSuperType() != null) {
            factory.setTypeAliasesSuperType(mybatisPlusProperties.getTypeAliasesSuperType());
        }
        if (StringUtils.hasLength(mybatisPlusProperties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(mybatisPlusProperties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(mybatisPlusProperties.resolveMapperLocations())) {
            factory.setMapperLocations(mybatisPlusProperties.resolveMapperLocations());
        }
        if (StringUtils.hasLength(mybatisPlusProperties.getTypeEnumsPackage())) {
            factory.setTypeEnumsPackage(mybatisPlusProperties.getTypeEnumsPackage());
        }
        GlobalConfig globalConfig = mybatisPlusProperties.getGlobalConfig();
        factory.setGlobalConfig(globalConfig);
        return factory.getObject();
    }

    public Integer getLogicDeleteValue() {
        return logicDeleteValue;
    }

    public Integer getLogicNotDeleteValue() {
        return logicNotDeleteValue;
    }
}
